<template>
  <div>
    <div class="cyjdiv el-icon-info">数据一共{{tableData.length}}条</div>
       <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="number" label="试题编号" width="150px">
          </el-table-column>
          <el-table-column prop="subject" label="学科" width="100px">
          </el-table-column>
          <el-table-column prop="catalog" label="目录" width="100px">
          </el-table-column>
          <el-table-column  label="题型" width="100px">
            <template #default="{row}">
              {{questionType.find(item=>item.value===+row.questionType).label}}
            </template>
          </el-table-column>
          <el-table-column label="题干" width="150px">
            <template #default="{row}">
            <div v-html="row.question"></div>
            </template>
          </el-table-column>
          <el-table-column prop="addDate" label="录入时间" width="200px">
          </el-table-column>
          <el-table-column  label="难度" width="100px">
            <template #default="{row}">
              {{difficulty.find(item=>item.value===+row.difficulty).label}}
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人" width="150px">
          </el-table-column>
          <el-table-column label="审核状态" width="100px">
            <template #default="{row}">
              {{chkType.find(item=>item.value===+row.chkState+1).label||'未知'}}
            </template>
          </el-table-column>
          <el-table-column prop="chkRemarks" label="审核意见" width="200px">
          </el-table-column>
          <el-table-column prop="chkUser" label="审核人" width="150px">
          </el-table-column>
          <el-table-column label="发布状态" width="100px">
            <template #default="{row}">
              {{publishType.find(item=>item.value===+row.publishState+1).label}}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200px">
              <template #default="{row}">
              <el-button type="text" size="small">预览</el-button>
              <el-button type="text" size="small" @click="openDialog(row.id)">审核</el-button>
              <el-button type="text" size="small" @click="$router.push('new')">修改</el-button>
              <el-button type="text" size="small" @click='choicePublish(row)'>{{row.publishState===1?'上架':'下架'}}</el-button>
              <el-button type="text" size="small" @click="removeTitle(row.id)">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
  </div>
</template>

<script>
import { questionType, difficulty, chkType, publishType } from '@/api/hmmm/constants.js'
import { remove as delQuestion, choicePublish } from '@/api/hmmm/questions.js'
export default {
  name: 'choiceCardTable', // 封装的表格组件名
  props: {
    tableData: { // 第一次搜索table表格所有数据
      type: Array,
      required: true
    }
  },
  data () {
    return {
      questionType, // 题型数据
      difficulty, // 难度数据
      chkType, // 审核状态数据
      publishType // 发布状态数据
    }
  },
  methods: {
    removeTitle (id) { // 点击删除事件
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      }).then(() => {
        delQuestion({ id: id }) // 发送删除请求
        this.$emit('removeTitle', id) // 通知父组件删除对应的数据
        this.$message.success('删除成功!')
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    choicePublish (row) { // 修改上下架事件
      this.$confirm('是否修改?', '提示', {
      }).then(() => {
        row.publishState = row.publishState === 1 ? 0 : 1 // 先判断题目的发布状态
        choicePublish(row) // 发送修改上下架请求
        this.$emit('RefreshThePage') // 刷新表格数据
        this.$message.success('修改成功!')
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    openDialog (id) { // 打开审核弹框事件
      this.$emit('openDialog', id)
    }
  }
}
</script>

<style lang='scss' scoped>
.cyjdiv {
  width: 100%;
  height: 30px;
  background-color: #f4f4f5;
  line-height: 30px;
  font-size: 13px;
  color: #909399;
  padding-left: 20px;
}
</style>
